import { getRouteGroup, getPmFormUser } from '../../../api/manage'
import create from 'dd-store'
import pageStore from '../../../stores/globalStore'
create.Page({
  store: pageStore,
  value: '',
  data: {
    options: [
    ],
    checkAllOptions: [
      {
        value: 'checkAll', label: '全选'
      }
    ],
    page: 1,
    size: 100,
    hasMore: true,
    isLoading: false,
    isCheckAll: false,
    checkAllValueText: '全选',
    pmFormId: null,
    selectedList: []
  },
  onLoad(options) {


    // console.log(options.pmFormId);
    this.setData({
      pmFormId: options.pmFormId
    })
    // console.log(this.$store.data.selectedList);
    this.loadData(true, 1)
    dd.showLoading({
      content: '加载中...'
    })



  },


  // 判断所有的选项是否全部选中的函数
  allIsCheckedAll() {
    let num = 0;
    this.data.options.forEach((item) => {
      if (item.isSelected) {
        num++;
      }
    })

    if (this.data.options.length == num) {
      // console.log('全部选中');
      this.setData({
        isCheckAll: true
      })
    } else {
      // console.log('全部选中');
      this.setData({
        isCheckAll: false
      })
    }
  },

  // 单个选中的事件函数
  changeCheck(e) {
    // console.log(this.$store.data);
    console.log('选中考评组', e);
    console.log(this.data.options);
    // 将选择的数据进行处理，然后放入selectedList当中
    let newArr = []
    e.detail.value.forEach((item) => {
      newArr.push({
        label: item,
        value: item,
        isSelected: true,
        pmGroupId: this.data.options.filter((itemP) => {
          return itemP.label == item
        })[0].pmGroupId
      })
    })
    this.setData({
      selectedList: newArr
    })
    console.log(e);
    // 刚进函数的时候，isSelected属性全部初始化为false
    this.data.options.forEach((item) => {
      item.isSelected = false
    })

    // 将选中的选项的数据的isChecked属性改为true
    this.data.options.forEach((item1) => {
      e.detail.value.forEach((item2) => {
        if (item1.label == item2) {
          item1.isSelected = true
        }
      })
    })
    // console.log(this.data.options);
    // console.log('单个选中');
    this.allIsCheckedAll()
  },
  // 全选选择框的change事件函数
  changeCheckAll(checked, e) {
    // 判断复选框的状态，勾选做什么处理，取消勾选做什么处理
    if (checked.detail.value) {
      // console.log('全选');
      this.data.options.forEach((item) => {
        item.isSelected = true
      })
      // console.log(this.data.options);
      this.$store.data.selectedList = this.data.options
      this.update()
    } else {
      // console.log('反选');
      this.data.options.forEach((item) => {
        item.isSelected = false
      })
    }
    // this.onLoad()
    // console.log(this.data.options);
    let data = this.data.options;
    // console.log(data);
    this.setData({
      options: data
    })
    this.allIsCheckedAll()
  },
  loadData(refresh = false) {

    this.setData({
      isLoading: true
    })
    // console.log(this.data.pmFormId, 'id');

    let data = {
      pmFormId: this.$store.data.pmFormId
    }
    getPmFormUser(data).then(rs => {
      // console.log(rs.data.pmGroupList);
      let newList = []
      rs.data.pmGroupList.forEach((item, index) => {
        newList.push({ label: item.pmGroupName, value: item.pmGroupName, isSelected: false, pmGroupId: item.pmGroupId })
      })
      // console.log(newList);
      this.setData({
        options: newList,
        isLoading: false,
      })
      // 获取到数据的时候判断数据是否上次被选中
      let newArr = newList
      console.log(newArr);
      console.log(this.$store.data.selectedList);
      newArr.forEach((item1) => {
        this.$store.data.selectedList.forEach((item2) => {
          if (item1.label == item2.label) {
            item1.isSelected = true
          }
        })
      })
      // 判断二次进入的时候上一次的是否全部命中数据，全部命中则默认全选
      let lg = 0
      newArr.forEach((item) => {
        if (item.isSelected == true) {
          lg++
        }
      })
      if (lg == newArr.length) {
        this.setData({
          isCheckAll: true
        })
      } else {
        this.setData({
          isCheckAll: false
        })
      }
      console.log(newArr);
      this.setData({
        options: newArr
      })
      dd.hideLoading()
    })

  },
  onChange(value, e) {
    this.setData({ value })
  },
  handleTap() {
    console.log(this.data.selectedList);
    // 清空store里面共享的数据
    this.$store.data.selectedList = []
    this.$store.data.userOrGroup = 'group'
    this.$store.data.selectedList = this.data.selectedList
    this.update()
    let pages = getCurrentPages();
    let prePage = pages[pages.length - 2];
    if (pages.length > 1) {
      let prePage = pages[pages.length - 2];
      prePage.changeFormGroup(this.data.value)
      // prePage.changeData(this.data.title);
      dd.navigateBack();
    }
    // console.log(pages, this.data.value, prePage);

  },
  cancel() {
    this.setData({
      value: ''
    })
    dd.navigateBack();
    // console.log('取消');
  },
  onReachBottom() {
    if (this.data.hasMore && !this.data.isLoading) {
      this.loadData(false, this.data.page + 1)
    }
    // if (this.data.hasMore && !this.data.isLoading) {
    //   this.loadForm(false, this.data.page + 1)
    // }
  },

  // 获取评分员工数据


});
